package com.huwaiwai.jvm.d20191227;

/**
 * @author 歪大哥😁
 * @date 2019-12-27.
 * 这个类是测试方法内联的
 */
public abstract class Passenger {

    /** 入境 */
    public abstract void entry();

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            doTest();
        }
    }

    /**
     * 测试方法内联带来的性能提升
     * 得到的数据为 1030 - 1290
     * 前1000000000次有内联作用调用耗费时间 约1030ms
     * 后1000000000次无内联作用调用耗费时间 约1290ms
     */
    private static void doTest() {
        Passenger chinese = new Chinese();
        Passenger foreigners = new Foreigners();
        long start = 0;
        for (int i = 1; i <= 2_000_000_000; i++) {
            if (i % 1_000_000_000 == 0) {
                long temp = System.currentTimeMillis();
                System.out.println(temp - start);
                start = temp;
            }
            Passenger passenger = i <= 1_000_000_000 ? chinese : foreigners;
            if (start == 0) {
                for (int h = 0; h < 10000; h++) {
                    passenger.entry();
                }
                start = System.currentTimeMillis();
            }
            passenger.entry();
        }
    }
}


class Chinese extends Passenger {

    @Override
    public void entry() {

    }
}

class Foreigners extends Passenger {
    @Override
    public void entry() {

    }
}